package com.sikaryofficial.qrcode.config;

import cn.hutool.core.util.StrUtil;
import com.sikaryofficial.qrcode.domain.vo.BrandUrl;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : qinjinyuan
 * @desc : 二维码跳转配置
 * @date : 2024/06/03 13:36
 */
@Getter
@Setter
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "hismk.qrcode.redirect")
@Slf4j
public class QrcodeRedirectConfig {
    /**
     * 主域名
     */
    private String domain;

    private List<BrandUrl> brandUrlList;

    private Map<String, String> brandUrlMap = new HashMap<>();

    public void setBrandUrlList(List<BrandUrl> brandUrlList) {
        this.brandUrlList = brandUrlList;
        buildBrandUrlMap();
    }

    private void buildBrandUrlMap() {
        brandUrlMap.clear();
        for (BrandUrl brandUrl : brandUrlList) {
            brandUrlMap.put(brandUrl.getBrandNo(), brandUrl.getUrl());
        }
    }

    public String getBrandUrl(String brandNo) {
        return brandUrlMap.getOrDefault(brandNo, null);
    }

    public String getBrandUrl(String brandNo, String p) {
        String url = getBrandUrl(brandNo);
        if (StrUtil.isNotBlank(url)) {
            return MessageFormat.format(url, p);
        }
        return null;
    }


}
